package com.example.controller;

import com.example.dto.ZyFlavorsDTO;
import com.example.excel.ZyFlavorsExcel;
import com.example.service.FlavorsService;

import com.example.model.api.Result;
import com.example.model.page.PageData;
import com.example.validator.AssertUtils;
import com.example.validator.ValidatorUtils;
import com.example.validator.group.AddGroup;
import com.example.validator.group.DefaultGroup;
import com.example.validator.group.UpdateGroup;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import jakarta.servlet.http.HttpServletResponse;
import com.example.utils.ExcelUtils;

/**
* 口味表
*
* @author yyyyjinying 1020066395@qq.com
* @since 1.0-SNAPSHOT 2025-04-29
*/
@RestController
@RequestMapping("flavors")
@Tag(name = "口味表")
public class FlavorsController {
    @Autowired
    private FlavorsService flavorsService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @RequestMapping(value = "page", method = {RequestMethod.POST, RequestMethod.GET})
    @PreAuthorize("hasAuthority('sys:dishflavors:page')")
    public Result<PageData<ZyFlavorsDTO>> page(@RequestParam Map<String, Object> params){
        PageData<ZyFlavorsDTO> page = flavorsService.page(params);

        return new Result<PageData<ZyFlavorsDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('sys:dishflavors:info')")
    public Result<ZyFlavorsDTO> get(@PathVariable("id") Long id){
        ZyFlavorsDTO data = flavorsService.get(id);

        return new Result<ZyFlavorsDTO>().ok(data);
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('sys:dishflavors:save')")
    public Result<String> save(@RequestBody ZyFlavorsDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        flavorsService.save(dto);

        return new Result<String>().ok("ok");
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('sys:dishflavors:update')")
    public Result<String> update(@RequestBody ZyFlavorsDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        flavorsService.update(dto);

        return new Result<String>().ok("ok");
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('sys:dishflavors:delete')")
    public Result<String> delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        flavorsService.delete(ids);

        return new Result<String>().ok("ok");
    }

    @GetMapping("export")
    @Operation(summary = "导出")
    @PreAuthorize("hasAuthority('sys:dishflavors:export')")
    public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<ZyFlavorsDTO> list = flavorsService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, "口味表", list, ZyFlavorsExcel.class);
    }

}